<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Cache;

use app\common\model\FirmStyleModel;

use app\common\model\FirmModel;
use app\common\model\StaffModel;

class Firm extends BaseController{
    public function detail(){
        $FirmModel = FirmModel::with('detail')->field('firm_id,firm_name')->find($this->request->firm_id);
        return success($FirmModel);
    }
    
    public function save(){
        $firm_name = $this->request->param('firm_name/s','');
        $logo = $this->request->param('logo/s','');
        $company_name = $this->request->param('company_name/s','');
        $info = $this->request->param('info/s','');
        $tel = $this->request->param('tel/s','');
        $address = $this->request->param('address/s','');
        
        $lat = $this->request->param('lat/s','');
        $lng = $this->request->param('lng/s','');
        
        $FirmModel = FirmModel::find($this->request->firm_id);
        
        $FirmModel->firm_name = $firm_name;
        $FirmModel->save();
        $FirmModel->detail()->save([
            'logo' => $logo,
            'company_name' => $company_name,
            'info' => $info,
            'tel' => $tel,
            'address' => $address,
            'lat' => $lat,
            'lng' => $lng,
        ]);
        return success('保存成功');
    }
    
    
    public function getStyle(){
        $template_id = $this->request->param('template_id/d',0);
        $style = FirmStyleModel::find($template_id > 0 ? $template_id : $this->request->firm_id);
        $style['staff'] = StaffModel::with(['detail','profile'])->where('firm_id',$this->request->firm_id)->where('is_delete',0)->find();
        return success($style);
    }
    public function saveStyle(){
        $global = $this->request->param('global/s','');
        $global = json_decode($global,true);

        $card = $this->request->param('card/s','');
        $card = json_decode($card,true);
        
        $menu = $this->request->param('menu/s','');
        $menu = json_decode($menu,true);
        
        $page = $this->request->param('page/s','');
        $page = json_decode($page,true);
        
        $style = FirmStyleModel::find($this->request->firm_id);
        if(empty($style)){
            $style = new FirmStyleModel();
            $style->firm_id = $this->request->firm_id;
        }
        $style->global = $global;
        $style->card = $card;
        $style->menu = $menu;
        $style->page = $page;
        $style->save();
        return success($style);
        
    }
    public function settingDetail(){
        $FirmModel = FirmModel::field('type,staff_num,expire_time')->find($this->request->firm_id);
        $FirmModel->total = StaffModel::where('firm_id',$this->request->firm_id)->where('is_delete',0)->count();
        return success($FirmModel);
    }
    public function settingSave(){
        if($this->request->role < 8){
            return error('无权限');
        }
        if(null === $FirmModel = FirmModel::find($this->request->firm_id)){
            return error('错误操作');
        }
        $expire_time = $this->request->param('expire_time/s','');
        if(!empty($expire_time)){
            $FirmModel->expire_time = $expire_time;
        }
        $type = $this->request->param('type/d',0);
        $FirmModel->type = $type;
        $staff_num = $this->request->param('staff_num/d',0);
        $FirmModel->staff_num = $staff_num;
        if($FirmModel->save()){
            return success('修改成功');
        }else{
            return error('修改失败');
        }
    }
}